<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/600718 (zh-CN, DDL); Windows/10.0.0 (Win64);"/>
  <meta name="content-class" content="yinxiang.markdown"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="822"/>

<div><span><div style="font-size: 14px; margin: 0; padding: 0; width: 100%;"><p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">1.Ribbon负载均衡是<strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">客户端均衡</strong>；<br/>
2.内置了多种<strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">负载均衡策略</strong>，内部负责复杂均衡的顶级接⼝为：<br/>
<strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">com.netflix.loadbalancer.IRule</strong>；</p>
<table style="margin: 2px 0 14px; color: #333; width: auto; border-collapse: collapse; box-sizing: border-box;"><thead style="line-height: 160%; box-sizing: content-box;"><tr style="line-height: 160%; box-sizing: content-box;"><th style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #72777b; border-top: 0; background-color: #7b8184; font-weight: 300; color: #fff; padding-top: 6px;">策略</th><th style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #72777b; border-top: 0; background-color: #7b8184; font-weight: 300; color: #fff; padding-top: 6px;">描述</th></tr></thead><tbody style="line-height: 160%; box-sizing: content-box;"><tr style="line-height: 160%; box-sizing: content-box;"><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">RoundRobinRule：轮询策略</td><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">默认超过10次获取到的server都不可⽤，会返回⼀个空的server</td></tr><tr style="line-height: 160%; box-sizing: content-box;"><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">RandomRule：随机策略</td><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">如果随机到的server为null或者不可⽤的话，会while不停的循环选取</td></tr><tr style="line-height: 160%; box-sizing: content-box;"><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">RetryRule：重试策略</td><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">⼀定时限内循环重试。默认继承RoundRobinRule，也⽀持⾃定义注⼊，RetryRule会在每次选取之后，对选举的server进⾏判断，是否为null，是否alive，并且在500ms内会不停的选取判断。⽽RoundRobinRule失效的策略是超过10次，RandomRule是没有失效时间的概念，只要serverList没都挂。</td></tr><tr style="line-height: 160%; box-sizing: content-box;"><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">BestAvailableRule：最⼩连接数策略</td><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">遍历serverList，选取出可⽤的且连接数最⼩的⼀个server。该算法⾥⾯有⼀个LoadBalancerStats的成员变量，会存储所有server的运⾏状况和连接数。如果选取到的server为null，那么会调⽤RoundRobinRule重新选取。1（1） 2（1）3（1）</td></tr><tr style="line-height: 160%; box-sizing: content-box;"><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">AvailabilityFilteringRule：可⽤过滤策略</td><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">扩展了轮询策略，会先通过默认的轮询选取⼀个server，再去判断该server是否超时可⽤，当前连接数是否超限，都成功再返回。</td></tr><tr style="line-height: 160%; box-sizing: content-box;"><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">ZoneAvoidanceRule：区域权衡策略（默认策略）</td><td style="line-height: 160%; box-sizing: content-box; padding: 5px 14px 5px 12px; border: 1px solid #eaeaea;">扩展了轮询策略，继承了2个过滤器：ZoneAvoidancePredicate和AvailabilityPredicate，除了过滤超时和链接数过多的server，还会过滤掉不符合要求的zone区域⾥⾯的所有节点，AWS --ZONE 在⼀个区域/机房内的服务实例中轮询</td></tr></tbody></table>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;"><strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">修改负载均衡策略</strong><br/>
<img src="01Ribbon负载均衡_files/Image.png" type="image/png" data-filename="Image.png"/><br/>
<strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">Ribbon核⼼源码剖析</strong><br/>
<img src="01Ribbon负载均衡_files/Image [1].png" type="image/png" data-filename="Image.png"/><br/>
<img src="01Ribbon负载均衡_files/Image [2].png" type="image/png" data-filename="Image.png"/><br/>
<strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">重点：Ribbon给restTemplate添加了⼀个拦截器</strong><br/>
负载均衡管理器LoadBalancer（总的协调者，相当于⼤脑，为了做事情，协调四肢），围绕它周围的多有IRule、IPing等<br/>
IRule：是在选择实例的时候的负载均衡策略对象<br/>
IPing：是⽤来向服务发起⼼跳检测的，通过⼼跳检测来判断该服务是否可⽤<br/>
ServerListFilter：根据⼀些规则过滤传⼊的服务实例列表<br/>
ServerListUpdater：定义了⼀系列的对服务列表的更新操作</p>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;"><strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">@LoadBalanced源码剖析</strong><br/>
添加了注解的RestTemplate对象会被添加⼀个拦截器LoadBalancerInterceptor，该拦截器就是后续拦截请求进⾏负载处理的。所以，下⼀步重点我们该分析拦截器LoadBalancerInterceptor------&gt;&gt;&gt;intercept()⽅法<br/>
接下来分析LoadBalancerInterceptor.intercept()⽅法<br/>
<img src="01Ribbon负载均衡_files/Image [3].png" type="image/png" data-filename="Image.png"/></p>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">RibbonLoadBalancerClient对象是在哪⾥注⼊的？<br/>
负载均衡的事情执⾏原来交给了我们最初看到的RibbonLoadBalancerClient对象</p>
</div><center style="display:none !important;visibility:collapse !important;height:0 !important;white-space:nowrap;width:100%;overflow:hidden">1.Ribbon%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E6%98%AF**%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%9D%87%E8%A1%A1**%EF%BC%9B%0A2.%E5%86%85%E7%BD%AE%E4%BA%86%E5%A4%9A%E7%A7%8D**%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%AD%96%E7%95%A5**%EF%BC%8C%E5%86%85%E9%83%A8%E8%B4%9F%E8%B4%A3%E5%A4%8D%E6%9D%82%E5%9D%87%E8%A1%A1%E7%9A%84%E9%A1%B6%E7%BA%A7%E6%8E%A5%E2%BC%9D%E4%B8%BA%EF%BC%9A%0A%20%20**com.netflix.loadbalancer.IRule**%EF%BC%9B%0A%0A%7C%20%E7%AD%96%E7%95%A5%20%7C%E6%8F%8F%E8%BF%B0%20%20%7C%0A%7C%20---%20%7C%20---%20%7C%0A%7C%20RoundRobinRule%EF%BC%9A%E8%BD%AE%E8%AF%A2%E7%AD%96%E7%95%A5%20%7C%20%20%E9%BB%98%E8%AE%A4%E8%B6%85%E8%BF%8710%E6%AC%A1%E8%8E%B7%E5%8F%96%E5%88%B0%E7%9A%84server%E9%83%BD%E4%B8%8D%E5%8F%AF%E2%BD%A4%EF%BC%8C%E4%BC%9A%E8%BF%94%E5%9B%9E%E2%BC%80%E4%B8%AA%E7%A9%BA%E7%9A%84server%7C%0A%7C%20RandomRule%EF%BC%9A%E9%9A%8F%E6%9C%BA%E7%AD%96%E7%95%A5%20%7C%20%E5%A6%82%E6%9E%9C%E9%9A%8F%E6%9C%BA%E5%88%B0%E7%9A%84server%E4%B8%BAnull%E6%88%96%E8%80%85%E4%B8%8D%E5%8F%AF%E2%BD%A4%E7%9A%84%E8%AF%9D%EF%BC%8C%E4%BC%9Awhile%E4%B8%8D%E5%81%9C%E7%9A%84%E5%BE%AA%E7%8E%AF%E9%80%89%E5%8F%96%20%7C%0A%7CRetryRule%EF%BC%9A%E9%87%8D%E8%AF%95%E7%AD%96%E7%95%A5%20%20%7C%E2%BC%80%E5%AE%9A%E6%97%B6%E9%99%90%E5%86%85%E5%BE%AA%E7%8E%AF%E9%87%8D%E8%AF%95%E3%80%82%E9%BB%98%E8%AE%A4%E7%BB%A7%E6%89%BFRoundRobinRule%EF%BC%8C%E4%B9%9F%E2%BD%80%E6%8C%81%E2%BE%83%E5%AE%9A%E4%B9%89%E6%B3%A8%E2%BC%8A%EF%BC%8CRetryRule%E4%BC%9A%E5%9C%A8%E6%AF%8F%E6%AC%A1%E9%80%89%E5%8F%96%E4%B9%8B%E5%90%8E%EF%BC%8C%E5%AF%B9%E9%80%89%E4%B8%BE%E7%9A%84server%E8%BF%9B%E2%BE%8F%E5%88%A4%E6%96%AD%EF%BC%8C%E6%98%AF%E5%90%A6%E4%B8%BAnull%EF%BC%8C%E6%98%AF%E5%90%A6alive%EF%BC%8C%E5%B9%B6%E4%B8%94%E5%9C%A8500ms%E5%86%85%E4%BC%9A%E4%B8%8D%E5%81%9C%E7%9A%84%E9%80%89%E5%8F%96%E5%88%A4%E6%96%AD%E3%80%82%E2%BD%BDRoundRobinRule%E5%A4%B1%E6%95%88%E7%9A%84%E7%AD%96%E7%95%A5%E6%98%AF%E8%B6%85%E8%BF%8710%E6%AC%A1%EF%BC%8CRandomRule%E6%98%AF%E6%B2%A1%E6%9C%89%E5%A4%B1%E6%95%88%E6%97%B6%E9%97%B4%E7%9A%84%E6%A6%82%E5%BF%B5%EF%BC%8C%E5%8F%AA%E8%A6%81serverList%E6%B2%A1%E9%83%BD%E6%8C%82%E3%80%82%20%20%7C%0A%7C%20%20BestAvailableRule%EF%BC%9A%E6%9C%80%E2%BC%A9%E8%BF%9E%E6%8E%A5%E6%95%B0%E7%AD%96%E7%95%A5%7C%20%E9%81%8D%E5%8E%86serverList%EF%BC%8C%E9%80%89%E5%8F%96%E5%87%BA%E5%8F%AF%E2%BD%A4%E7%9A%84%E4%B8%94%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%9C%80%E2%BC%A9%E7%9A%84%E2%BC%80%E4%B8%AAserver%E3%80%82%E8%AF%A5%E7%AE%97%E6%B3%95%E2%BE%A5%E2%BE%AF%E6%9C%89%E2%BC%80%E4%B8%AALoadBalancerStats%E7%9A%84%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F%EF%BC%8C%E4%BC%9A%E5%AD%98%E5%82%A8%E6%89%80%E6%9C%89server%E7%9A%84%E8%BF%90%E2%BE%8F%E7%8A%B6%E5%86%B5%E5%92%8C%E8%BF%9E%E6%8E%A5%E6%95%B0%E3%80%82%E5%A6%82%E6%9E%9C%E9%80%89%E5%8F%96%E5%88%B0%E7%9A%84server%E4%B8%BAnull%EF%BC%8C%E9%82%A3%E4%B9%88%E4%BC%9A%E8%B0%83%E2%BD%A4RoundRobinRule%E9%87%8D%E6%96%B0%E9%80%89%E5%8F%96%E3%80%821%EF%BC%881%EF%BC%89%202%EF%BC%881%EF%BC%893%EF%BC%881%EF%BC%89%20%7C%0A%7CAvailabilityFilteringRule%EF%BC%9A%E5%8F%AF%E2%BD%A4%E8%BF%87%E6%BB%A4%E7%AD%96%E7%95%A5%20%20%7C%20%E6%89%A9%E5%B1%95%E4%BA%86%E8%BD%AE%E8%AF%A2%E7%AD%96%E7%95%A5%EF%BC%8C%E4%BC%9A%E5%85%88%E9%80%9A%E8%BF%87%E9%BB%98%E8%AE%A4%E7%9A%84%E8%BD%AE%E8%AF%A2%E9%80%89%E5%8F%96%E2%BC%80%E4%B8%AAserver%EF%BC%8C%E5%86%8D%E5%8E%BB%E5%88%A4%E6%96%AD%E8%AF%A5server%E6%98%AF%E5%90%A6%E8%B6%85%E6%97%B6%E5%8F%AF%E2%BD%A4%EF%BC%8C%E5%BD%93%E5%89%8D%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%98%AF%E5%90%A6%E8%B6%85%E9%99%90%EF%BC%8C%E9%83%BD%E6%88%90%E5%8A%9F%E5%86%8D%E8%BF%94%E5%9B%9E%E3%80%82%20%7C%0A%7CZoneAvoidanceRule%EF%BC%9A%E5%8C%BA%E5%9F%9F%E6%9D%83%E8%A1%A1%E7%AD%96%E7%95%A5%EF%BC%88%E9%BB%98%E8%AE%A4%E7%AD%96%E7%95%A5%EF%BC%89%20%7C%E6%89%A9%E5%B1%95%E4%BA%86%E8%BD%AE%E8%AF%A2%E7%AD%96%E7%95%A5%EF%BC%8C%E7%BB%A7%E6%89%BF%E4%BA%862%E4%B8%AA%E8%BF%87%E6%BB%A4%E5%99%A8%EF%BC%9AZoneAvoidancePredicate%E5%92%8CAvailabilityPredicate%EF%BC%8C%E9%99%A4%E4%BA%86%E8%BF%87%E6%BB%A4%E8%B6%85%E6%97%B6%E5%92%8C%E9%93%BE%E6%8E%A5%E6%95%B0%E8%BF%87%E5%A4%9A%E7%9A%84server%EF%BC%8C%E8%BF%98%E4%BC%9A%E8%BF%87%E6%BB%A4%E6%8E%89%E4%B8%8D%E7%AC%A6%E5%90%88%E8%A6%81%E6%B1%82%E7%9A%84zone%E5%8C%BA%E5%9F%9F%E2%BE%A5%E2%BE%AF%E7%9A%84%E6%89%80%E6%9C%89%E8%8A%82%E7%82%B9%EF%BC%8CAWS%20--ZONE%20%E5%9C%A8%E2%BC%80%E4%B8%AA%E5%8C%BA%E5%9F%9F%2F%E6%9C%BA%E6%88%BF%E5%86%85%E7%9A%84%E6%9C%8D%E5%8A%A1%E5%AE%9E%E4%BE%8B%E4%B8%AD%E8%BD%AE%E8%AF%A2%20%7C%0A**%E4%BF%AE%E6%94%B9%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%AD%96%E7%95%A5**%0A!%5B3331d2e6c30ca091257c5d44e5c57cb4.png%5D(en-resource%3A%2F%2Fdatabase%2F1133%3A0)%0A**Ribbon%E6%A0%B8%E2%BC%BC%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90**%0A!%5B280e037208320f7fe79bfe1572ad34d9.png%5D(en-resource%3A%2F%2Fdatabase%2F1135%3A0)%0A!%5Ba054a76a4fad22a8b66587f973d2271e.png%5D(en-resource%3A%2F%2Fdatabase%2F1137%3A0)%0A**%E9%87%8D%E7%82%B9%EF%BC%9ARibbon%E7%BB%99restTemplate%E6%B7%BB%E5%8A%A0%E4%BA%86%E2%BC%80%E4%B8%AA%E6%8B%A6%E6%88%AA%E5%99%A8**%0A%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%AE%A1%E7%90%86%E5%99%A8LoadBalancer%EF%BC%88%E6%80%BB%E7%9A%84%E5%8D%8F%E8%B0%83%E8%80%85%EF%BC%8C%E7%9B%B8%E5%BD%93%E4%BA%8E%E2%BC%A4%E8%84%91%EF%BC%8C%E4%B8%BA%E4%BA%86%E5%81%9A%E4%BA%8B%E6%83%85%EF%BC%8C%E5%8D%8F%E8%B0%83%E5%9B%9B%E8%82%A2%EF%BC%89%EF%BC%8C%E5%9B%B4%E7%BB%95%E5%AE%83%E5%91%A8%E5%9B%B4%E7%9A%84%E5%A4%9A%E6%9C%89IRule%E3%80%81IPing%E7%AD%89%0AIRule%EF%BC%9A%E6%98%AF%E5%9C%A8%E9%80%89%E6%8B%A9%E5%AE%9E%E4%BE%8B%E7%9A%84%E6%97%B6%E5%80%99%E7%9A%84%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%AD%96%E7%95%A5%E5%AF%B9%E8%B1%A1%0AIPing%EF%BC%9A%E6%98%AF%E2%BD%A4%E6%9D%A5%E5%90%91%E6%9C%8D%E5%8A%A1%E5%8F%91%E8%B5%B7%E2%BC%BC%E8%B7%B3%E6%A3%80%E6%B5%8B%E7%9A%84%EF%BC%8C%E9%80%9A%E8%BF%87%E2%BC%BC%E8%B7%B3%E6%A3%80%E6%B5%8B%E6%9D%A5%E5%88%A4%E6%96%AD%E8%AF%A5%E6%9C%8D%E5%8A%A1%E6%98%AF%E5%90%A6%E5%8F%AF%E2%BD%A4%0AServerListFilter%EF%BC%9A%E6%A0%B9%E6%8D%AE%E2%BC%80%E4%BA%9B%E8%A7%84%E5%88%99%E8%BF%87%E6%BB%A4%E4%BC%A0%E2%BC%8A%E7%9A%84%E6%9C%8D%E5%8A%A1%E5%AE%9E%E4%BE%8B%E5%88%97%E8%A1%A8%0AServerListUpdater%EF%BC%9A%E5%AE%9A%E4%B9%89%E4%BA%86%E2%BC%80%E7%B3%BB%E5%88%97%E7%9A%84%E5%AF%B9%E6%9C%8D%E5%8A%A1%E5%88%97%E8%A1%A8%E7%9A%84%E6%9B%B4%E6%96%B0%E6%93%8D%E4%BD%9C%0A%0A**%40LoadBalanced%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90**%0A%E6%B7%BB%E5%8A%A0%E4%BA%86%E6%B3%A8%E8%A7%A3%E7%9A%84RestTemplate%E5%AF%B9%E8%B1%A1%E4%BC%9A%E8%A2%AB%E6%B7%BB%E5%8A%A0%E2%BC%80%E4%B8%AA%E6%8B%A6%E6%88%AA%E5%99%A8LoadBalancerInterceptor%EF%BC%8C%E8%AF%A5%E6%8B%A6%E6%88%AA%E5%99%A8%E5%B0%B1%E6%98%AF%E5%90%8E%E7%BB%AD%E6%8B%A6%E6%88%AA%E8%AF%B7%E6%B1%82%E8%BF%9B%E2%BE%8F%E8%B4%9F%E8%BD%BD%E5%A4%84%E7%90%86%E7%9A%84%E3%80%82%E6%89%80%E4%BB%A5%EF%BC%8C%E4%B8%8B%E2%BC%80%E6%AD%A5%E9%87%8D%E7%82%B9%E6%88%91%E4%BB%AC%E8%AF%A5%E5%88%86%E6%9E%90%E6%8B%A6%E6%88%AA%E5%99%A8LoadBalancerInterceptor------%3E%3E%3Eintercept()%E2%BD%85%E6%B3%95%0A%E6%8E%A5%E4%B8%8B%E6%9D%A5%E5%88%86%E6%9E%90LoadBalancerInterceptor.intercept()%E2%BD%85%E6%B3%95%0A!%5B50a855ef8af3b39a1153d64e85470901.png%5D(en-resource%3A%2F%2Fdatabase%2F1139%3A0)%0A%0ARibbonLoadBalancerClient%E5%AF%B9%E8%B1%A1%E6%98%AF%E5%9C%A8%E5%93%AA%E2%BE%A5%E6%B3%A8%E2%BC%8A%E7%9A%84%EF%BC%9F%0A%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%9A%84%E4%BA%8B%E6%83%85%E6%89%A7%E2%BE%8F%E5%8E%9F%E6%9D%A5%E4%BA%A4%E7%BB%99%E4%BA%86%E6%88%91%E4%BB%AC%E6%9C%80%E5%88%9D%E7%9C%8B%E5%88%B0%E7%9A%84RibbonLoadBalancerClient%E5%AF%B9%E8%B1%A1%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A</center></span>
</div></body></html> 